package problems;

import java.math.BigInteger;

public class Euler188 extends AbstractEuler {

	@Override
	public Number calculate() {
		BigInteger base = BigInteger.valueOf(1777);
		BigInteger answer = base;
		BigInteger last8Digits = BigInteger.valueOf(100000000);
		for (int i = 1; i <= 1855; i++) {
			answer = base.modPow(answer, last8Digits);
		}
		return answer.mod(last8Digits).intValue();
	}
	
	public void testTetrationWithModPow() {
		BigInteger mod = BigInteger.valueOf(10);
		
		BigInteger three = BigInteger.valueOf(3);
		BigInteger threeTetrateThree = three.pow(27);
		assertEquals(7625597484987L, threeTetrateThree.longValue());
		
		BigInteger threeTetrateThreeMod = three.modPow(three.modPow(three, mod), mod);
		assertEquals(threeTetrateThree.longValue() % 10, threeTetrateThreeMod.intValue());
		
		// passing this test corroborates Euler's theorem:
		// http://en.wikipedia.org/wiki/Euler%27s_theorem
	}

	@Override
	protected Number getCorrectAnswer() {
		return 95962097;
	}

}
